#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *filename = "dup_test.txt";
    int fd = open(filename, O_CREAT | O_RDWR, 0644);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    // 复制文件描述符（最小值为 10）
    int new_fd = fcntl(fd, F_DUPFD, 10);
    if (new_fd == -1) {
        perror("fcntl F_DUPFD");
        close(fd);
        exit(EXIT_FAILURE);
    }

    // 验证新描述符可写
    if (write(new_fd, "Hello from new fd", 17) == -1) {
        perror("write new_fd");
    } else {
        printf("F_DUPFD 测试通过！新描述符: %d\n", new_fd);
    }

    close(fd);
    close(new_fd);
    remove(filename); // 清理测试文件
    return EXIT_SUCCESS;
}
